﻿#include "QStackedManageBase.h"
#include "QStackedFrameBase.h"

QStackedManageBase::QStackedManageBase(QWidgetBase *parent) : QWidgetBase(parent)
{
	this->setWindowFlags(Qt::FramelessWindowHint);
	m_ManageBase = this;
}

QStackedManageBase::~QStackedManageBase()
{

}

// 创建初始化UI
void QStackedManageBase::initUI()
{
	m_pQStackedLayout =  new QStackedLayout(this);
	this->setLayout(m_pQStackedLayout);

	m_pQTimer = new QTimer(this);
}

// 初始化样式
void QStackedManageBase::initStyleSheet()
{

}

// 初始化连接
void QStackedManageBase::initConnect()
{
	connect(m_pQTimer, SIGNAL(timeout()), this, SLOT(slotsTimerOut()));
}

// 页面启动
void QStackedManageBase::onStart()
{
	QStackedFrameBase *layout = (QStackedFrameBase *)m_pQStackedLayout->currentWidget();
	if(layout)
	{
		layout->onStart();
	}
}

// 页面停止
void QStackedManageBase::onStop()
{
	QStackedFrameBase *layout = (QStackedFrameBase *)m_pQStackedLayout->currentWidget();
	if(layout)
		layout->onStop();
}

// 定时超时
void QStackedManageBase::slotsTimerOut()
{
	QStackedFrameBase *layout = (QStackedFrameBase *)m_pQStackedLayout->currentWidget();
	if(layout)
		layout->timeOutHander();
}

//超时返回定时器启动函数
void QStackedManageBase::timerStart(int timeMs)
{
	m_pQTimer->start(timeMs);
}

//超时返回定时器停止函数
void QStackedManageBase::timerStop()
{
	m_pQTimer->stop();
}

// 显示帧视图
QStackedFrameBase* QStackedManageBase::showFrameLayout(QString layout)
{
	qDebug() << layout << "start!!!!";
	QMap<QString,QWidget*>::Iterator iter = m_QStackedMap.find(layout);
	if(iter != m_QStackedMap.end()){

		QStackedFrameBase *thisLayout = (QStackedFrameBase *)m_pQStackedLayout->currentWidget();
		thisLayout->onStop();
		m_pQStackedLayout->setCurrentWidget(iter.value());
		QStackedFrameBase *layoutWidget = (QStackedFrameBase *)m_pQStackedLayout->currentWidget();
		layoutWidget->onStart();
		int timeOut = layoutWidget->getTimeOut();
		if(timeOut > 0) {
			timerStart(timeOut * 1000);
		} else {
			timerStop();
		}
		// 聚焦当前视图
		layoutWidget->setFocus();
		qDebug() << layoutWidget << "start end!!!!";
		return layoutWidget;
	}
	qDebug("%s is not add",layout.toStdString().data());
	return nullptr;
}

// 判断视图是否存在
bool QStackedManageBase::isFrameLayoutExist(QString layout)
{
	return m_QStackedMap.contains(layout);
}

// 添加帧视图
void QStackedManageBase::addFrameLayout(QString layout,QStackedFrameBase *frameLayout)
{
	 m_pQStackedLayout->addWidget(frameLayout);
	m_QStackedMap[layout] = frameLayout;
}

// 删除帧视图
bool QStackedManageBase::deleteFrameLayout(QString layout,QString frameLayout)
{
	QMap<QString, QWidget*>::iterator i = m_QStackedMap.find(layout);
	qDebug() << layout;
	qDebug() << m_QStackedMap.keys();
	qDebug() <<  m_QStackedMap.values();
	if( i != m_QStackedMap.end() && i.key() == layout )
	{
		QWidget * widget = m_QStackedMap[layout];
		m_QStackedMap.erase(i);
		if(widget){
			m_pQStackedLayout->removeWidget(widget);
//			delteLayout->deleteLater();
			widget->close();
			if(!frameLayout.isEmpty()){
				QMap<QString,QWidget*>::Iterator iter = m_QStackedMap.find(layout);
				if(iter != m_QStackedMap.end()){
					m_pQStackedLayout->setCurrentWidget(iter.value());
				}
			}
		}
		return true;
	}
	return false;
}

